﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System;

namespace Sustainalytics.DynamicsCRMGateway
{
    public class CrmServiceContextFactory
    {
        public CrmServiceContext GetCrmServiceContext(string connectionString)
        {
            try
            {
                var crmServiceContext = new CrmServiceContext(CrmConnection.Parse(connectionString));
                crmServiceContext.TryAccessCache(cache => cache.Mode = OrganizationServiceCacheMode.Disabled);

                return crmServiceContext;
            }
            catch (Exception ex)
            {
                throw new CrmException("Error creating CRM service context.", ex);
            }
        }
    }
}
